package com.example.teamwork.impl;

import com.example.teamwork.Shrio.Exception.ServiceException;
import com.example.teamwork.informationGet.PageVO;
import com.example.teamwork.mapper.OrderMapper;
import com.example.teamwork.pojo.Order;
import com.example.teamwork.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 〈订单管理〉<br>
 * 〈〉
 * @author  SongPeng
 * @date  2021/11/06 14:24
 * @version 1.0
 */

@Service
public class OrderServiceImpl implements OrderService {
    private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);
    @Autowired
    private OrderMapper orderMapper;
    /**
     * 添加访客预约记录
     * */
    @Override
    public void add(Order order) {
        logger.info("添加访客预约");
        order.setOrderCreatetime(new Date());
        order.setOrderCtype("身份证");
        List<Order> orderList;
        Example o = new Example(Order.class);
        String orderCnumber = order.getOrderCnumber();
        Example.Criteria criteria = o.createCriteria();
        criteria.andEqualTo("orderCnumber", orderCnumber);
        orderList = orderMapper.selectByExample(o);
        System.out.println(orderList.size());
        if (orderList.size() != 0) {
            throw new ServiceException("该用户已经预约过了");
        } else {
            order.setOrderStatus(0);
            orderMapper.insert(order);
        }
    }
    /**
     * 当前访客预约列表
     *
     * */
    @Override
    public PageVO<Order> findOrderList(Integer pageNum, Integer pageSize, Order order,Date startTime,Date endTime) {
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Order.class);
        String orderCnumber=order.getOrderCnumber();
        Example.Criteria criteria = o.createCriteria();
        if(orderCnumber!=null&&!"".equals(orderCnumber)){
            criteria.andLike("orderCnumber","%"+orderCnumber+"%");
        }
        criteria.andNotEqualTo("orderStatus",1);
        if(startTime!=null&&!"".equals(startTime) ) {
            criteria.andBetween("orderCreatetime", startTime, endTime);
        }
        List<Order> orderList = orderMapper.selectByExample(o);
        PageInfo<Order> info=new PageInfo<>(orderList);
        return new PageVO<>(info.getTotal(),orderList);
    }
    @Override
    public PageVO<Order> findTodayList(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Order.class);
        Example.Criteria criteria = o.createCriteria();
        o.setOrderByClause("order_visittime asc");
        criteria.andGreaterThan("orderVisittime", new Date());
        List<Order> orderList = orderMapper.selectByExample(o);
        PageInfo<Order> info=new PageInfo<>(orderList);
        return new PageVO<>(info.getTotal(),orderList);
    }
}
